package com.hbx.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hbx.eduservice.entity.EduChapter;
import com.hbx.eduservice.entity.EduVideo;
import com.hbx.eduservice.entity.chapter.ChapterVo;
import com.hbx.eduservice.entity.chapter.VideoVo;
import com.hbx.eduservice.entity.dto.CourseInfoVo;
import com.hbx.eduservice.mapper.EduChapterMapper;
import com.hbx.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbx.eduservice.service.EduVideoService;
import com.hbx.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author hbx
 * @since 2023-10-02
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService videoService;
    /**
     * 根据课程id查询章节和小节
     * @param courseId
     * @return
     */
    @Override
    @Transactional
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {
        //1 根据课程id查询课程里面所有的章节
        LambdaQueryWrapper<EduChapter> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EduChapter::getCourseId,courseId);
        List<EduChapter> chapters = this.list(queryWrapper);
        List<ChapterVo> chapterVos = new ArrayList<>();
        for (int i = 0; i < chapters.size(); i++) {
            ChapterVo chapterVo = new ChapterVo();
            chapterVo.setId(chapters.get(i).getId());
            chapterVo.setTitle(chapters.get(i).getTitle());
            chapterVos.add(chapterVo);
        }
        //2 根据课程id查询课程里面所有的小杰
        LambdaQueryWrapper<EduVideo> queryWrapper1 = new LambdaQueryWrapper<>();
        List<EduVideo> videos = videoService.list(queryWrapper1);
        //3 遍历 封装chapterVos
        for (int i = 0; i < chapterVos.size(); i++) {
            for (int j = 0; j < videos.size(); j++) {
                if(chapterVos.get(i).getId().equals(videos.get(j).getChapterId())){
                    VideoVo videoVo = new VideoVo();
                    videoVo.setId(videos.get(j).getId());
                    videoVo.setTitle(videos.get(j).getTitle());
                    videoVo.setVideoSourceId(videos.get(j).getVideoSourceId());
                    chapterVos.get(i).getChildren().add(videoVo);
                }
            }
        }

        return chapterVos;
    }

    /**
     * 删除章节
     * @param chapterId
     */
    @Override
    public boolean deleteChapter(String chapterId) {
        LambdaQueryWrapper<EduVideo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EduVideo::getChapterId,chapterId);
        int count = videoService.count(queryWrapper);
        if(count > 0){
            throw new GuliException(20001,"章节下存在小节，不能删除");
        }else{
            int result = baseMapper.deleteById(chapterId);
            return result>0;
        }
    }
}
